package com.leetcode.No0777;

import org.junit.Test;

import java.util.Arrays;

/**
 * @program: leetcode
 * @description: 在LR字符串中交换相邻字符
 * @author: wangzhihua
 * @date: 2022-10-18
 */
public class Solution3 {
	public boolean canTransform(String start, String end) {
		int len = start.length();

		int x = 0, y = 0;
		while (x < len && y < len) {
			while (x < len && start.charAt(x) == 'X') {
				++x;
			}
			while (y < len && end.charAt(y) == 'X') {
				++y;
			}
			if (x >= len || y >= len) {
				break;
			}
			if (start.charAt(x) != end.charAt(y)) {
				return false;
			}
			if ((start.charAt(x) == 'L' && x < y) || (start.charAt(x) == 'R' && x > y)) {
				return false;
			}
			++x; ++y;
		}
		while (x < len) {
			if (start.charAt(x) != 'X') {
				return false;
			}
			++x;
		}
		while (y < len) {
			if (end.charAt(y) != 'X') {
				return false;
			}
			++y;
		}
		return true;
	}

	@Test
	public void test1() {
		// true
		String start = "RXXLRXRXL";
		String end = "XRLXXRRLX";
//		System.out.println(this.canTransform(start, end));

		// true
		start = "RXXLRXRXLX";
		end = "XRLXXRRLXX";
//		System.out.println(this.canTransform(start, end));

		// false
		start = "RXXXXXLRXRXLX";
		end = "XRXLXLXXRRLXX";
//		System.out.println(this.canTransform(start, end));

		// false
		start = "X";
		end = "L";
//		System.out.println(this.canTransform(start, end));

		// false
		start = "XLLR";
		end =   "LXLX";
//		System.out.println(this.canTransform(start, end));

		// false
		start = "LXXLXRLXXL";
		end =   "XLLXRXLXLX";
//		System.out.println(this.canTransform(start, end));

		// true
		start = "RXXLRXRXL";
		end =   "XRLXXRRLX";
//		System.out.println(this.canTransform(start, end));

		// true
		start = "XXXXXLXXXX";
		end =   "LXXXXXXXXX"; 
//		System.out.println(this.canTransform(start, end));

		// false
		start = "XRXXXLXXXR";
		end =   "XXRLXXXRXX";
		System.out.println(this.canTransform(start, end));
	}
}
